package com.qu.waibao.utils;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestFutureTarget;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by quguangle on 2018/7/20.
 * email 1123599561@qq.com
 */

public class ImageLoader {
    private ImageLoader() {

    }

    public static void loadImage(RequestManager loader, ImageView view, String url) {
        loadImage(loader, view, url, 0);
    }

    public static void loadImage(RequestManager loader, ImageView view, String url, int placeholder) {
        loadImage(loader, view, url, placeholder, placeholder);
    }

    public static void loadImage(RequestManager loader, ImageView view, String url, int placeholder, int error) {
        boolean isCenterCrop = false;
        if (view instanceof CircleImageView)
            isCenterCrop = true;
        loadImage(loader, view, url, placeholder, error, isCenterCrop);
    }

    public static void loadImage(RequestManager loader, ImageView view, String url, int placeholder, int error, boolean isCenterCrop) {
        if (TextUtils.isEmpty(url)) {
            view.setImageResource(placeholder);
        } else {
            if (view instanceof CircleImageView) {
                RequestOptions options = new RequestOptions();
                options.error(error)
                        .placeholder(placeholder)
                        .circleCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                if (isCenterCrop)
                    options.centerCrop();
                loader.load(url).apply(options).into(view);

            } else {
                RequestOptions options = new RequestOptions();
                options.error(error)
                        .placeholder(placeholder)
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                if(isCenterCrop){
                    options.centerCrop();
                }
                loader.load(url).apply(options).into(view);
            }
        }
    }
}
